package com.pvj.myaccount.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.RecyclerView
import com.pvj.myaccount.R
import com.pvj.myaccount.room.entity.MonthCost
import com.pvj.myaccount.swipereveallayout.SwipeRevealLayout
import com.pvj.myaccount.swipereveallayout.ViewBinderHelper
import com.pvj.myaccount.uitls.TimeUtil
import kotlin.random.Random


class MonthCostRecyclerViewAdapter : RecyclerView.Adapter<MonthCostRecyclerViewAdapter.MyViewHolder>() {
    private var dataList: List<MonthCost> = listOf()
    private var onItemClickListener: OnItemClickListener? = null

    private val binderHelper = ViewBinderHelper()


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.adapter_item, parent, false)
        return MyViewHolder(view)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        binderHelper.bind(holder.swipeLayout, dataList[position].id.toString())
        holder.bind(dataList[position])

        // Use ViewBindHelper to restore and save the open/close state of the SwipeRevealView
        // put an unique string id as value, can be any string which uniquely define the data

    }

    override fun getItemCount(): Int = dataList.size

    @SuppressLint("NotifyDataSetChanged")
    fun submitList(items: List<MonthCost>?) {
        if (items == null) {
            this.dataList = listOf()
        } else {
            this.dataList = items
        }
        notifyDataSetChanged()
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val swipeLayout: SwipeRevealLayout = view.findViewById(R.id.swipe_layout)
        private val nameTextView: TextView = view.findViewById(R.id.nameTextView)
        private val moneyTextView: TextView = view.findViewById(R.id.moneyTextView)
        private val updateBtn: TextView = view.findViewById(R.id.update_btn)
        private val deleteBtn: TextView = view.findViewById(R.id.delete_btn)
        private val imageView: ImageView = view.findViewById(R.id.image_view)
        private val timeTv: TextView = view.findViewById(R.id.time_tv)
        private val regularTv: TextView = view.findViewById(R.id.regular_tv)
        private val typeTv: TextView = view.findViewById(R.id.type_tv)

        @SuppressLint("SetTextI18n")
        fun bind(data: MonthCost) {
            typeTv.text = data.type
            typeTv.setBackgroundColor(getRandomColor())
            nameTextView.text = data.name
            moneyTextView.text = "-" + data.money.toString()


            val imgId = when (data.type) {
                in "生活用品" -> R.mipmap.ic_life_used
                in "饮食" -> R.mipmap.ic_food
                in "水果" -> R.mipmap.ic_shuiguo
                in "妇幼" -> R.mipmap.ic_fuyou
                in "房租" -> R.mipmap.ic_hours_cost
                in "话费/电信" -> R.mipmap.ic_net
                in "贷款" -> R.mipmap.ic_daikuang
                in "交通出行" -> R.mipmap.ic_jiaotong
                else -> R.mipmap.ic_other
            }
            imageView.setImageResource(imgId)

            regularTv.visibility =  View.GONE

            updateBtn.setOnClickListener {
                onItemClickListener?.onUpdate(data)
            }
            deleteBtn.setOnClickListener {
                showConfirmationDialog(it.context,data)
            }
        }
    }

    fun getRandomColor(): Int {
        return Color.rgb(Random.nextInt(256), Random.nextInt(256), Random.nextInt(256))
    }

    private fun showConfirmationDialog(context: Context, data: MonthCost) {
        val builder = AlertDialog.Builder(context)

        builder.setTitle("确定要删除吗？")
            .setMessage("这是您要确认的内容吗？\n ${data.name}:${data.money}")
            .setCancelable(false)
            .setPositiveButton("确定", DialogInterface.OnClickListener { _, _ ->
                // 当用户点击“确定”按钮时执行的操作
                // 这里可以放你的业务逻辑代码
                onItemClickListener?.onDelete(data)
            })
            .setNegativeButton("取消", DialogInterface.OnClickListener { _, _ ->
                // 当用户点击“取消”按钮时执行的操作
                // 这里也可以放一些清理工作或者其他的逻辑代码
                //println("取消被点击")
            })

        val alert = builder.create()
        alert.show()
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.onItemClickListener = listener
    }

    interface OnItemClickListener {

        fun onUpdate(data: MonthCost)

        fun onDelete(data: MonthCost)
    }
}


